import pandas as pd
from ..core.base_strategy import BaseStrategy

class MovingAverageCrossover(BaseStrategy):
    """双均线交叉策略"""
    
    def required_factors(self):
        return ['close']  # 需要收盘价数据
    
    def initialize(self, initial_capital: float):
        self.cash = initial_capital
        self.position = {}
        self.short_window = self.params.get('short_window', 10)
        self.long_window = self.params.get('long_window', 30)
        
    def generate_signal(self, price_data: pd.DataFrame, _) -> pd.DataFrame:
        signals = []
        for symbol, df in price_data.groupby('symbol'):
            close = df['close']
            short_ma = close.rolling(self.short_window).mean()
            long_ma = close.rolling(self.long_window).mean()
            
            # 生成信号
            if short_ma.iloc[-1] > long_ma.iloc[-1]:
                signal = 0.1  # 10%仓位
            elif short_ma.iloc[-1] < long_ma.iloc[-1]:
                signal = -0.5  # 平半仓
            else:
                signal = 0
                
            signals.append({
                'symbol': symbol,
                'datetime': df.index[-1],
                'signal': signal,
                'strength': abs(short_ma.iloc[-1] - long_ma.iloc[-1])
            })
        return pd.DataFrame(signals)
    
    def get_parameters(self):
        return self.params
    
    def version(self):
        return f"MACross_v{self.short_window}_{self.long_window}"